package com.kymatrix.kycarbon.xsmd.common.business.oss.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kymatrix.kycarbon.xsmd.common.business.oss.service.OssService;
import com.kymatrix.kycarbon.xsmd.common.business.oss.dao.OssMapper;
import com.kymatrix.kycarbon.xsmd.common.business.oss.model.entity.OssFile;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import java.io.IOException;
import java.io.InputStream;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class OssServiceImpl extends ServiceImpl<OssMapper, OssFile> implements OssService {

    @Resource
    private MinioClient minioClient;

    @Value("${minio.bucketName}")
    private String bucketName;

    @Value("${minio.minio_url_outer}")
    private String address;

    /**
     * configUpload
     */
    @Override
    public OssFile configUpload(MultipartFile multipartFile)  {
        final OssFile ossFile = new OssFile();
        String fileName = multipartFile.getOriginalFilename();
        if (fileName == null) {
            fileName = "";
        }
        String[] split = fileName.split("\\.");
        if (split.length > 1) {
            fileName = split[0] + "_" + System.currentTimeMillis() + "." + split[1];
        } else {
            fileName = fileName + System.currentTimeMillis();
        }
        InputStream in = null;
        try {
            in = multipartFile.getInputStream();
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(fileName)
                    .stream(in, in.available(), -1)
                    .contentType(multipartFile.getContentType())
                    .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        ossFile.setFileName(fileName);
        ossFile.setUrl(address + "/" + bucketName + "/" + fileName);
        this.save(ossFile);
        return ossFile;
    }


    @Override
    public OssFile upload(InputStream in, String fileName) {
        fileName = fileName + System.currentTimeMillis() + ".xlsx";
        OssFile ossFile = new OssFile();
        try {
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(fileName)
                    .stream(in, in.available(), -1)
                    .contentType("application/vnd.ms-excel")
                    .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        ossFile.setFileName(fileName);
        ossFile.setUrl(address + "/" + bucketName + "/" + fileName);
        return ossFile;

    }

    @Override
    public OssFile uploadFile(InputStream in, String fileName) {
        OssFile ossFile = new OssFile();
        try {
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(fileName)
                    .stream(in, in.available(), -1)
                    .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        ossFile.setFileName(fileName);
        ossFile.setUrl(address + "/" + bucketName + "/" + fileName);
        return ossFile;

    }

    @Override
    public InputStream getFileByUrl(String url) {
        String prefix = address + "/" + bucketName + "/";
        if (url.contains(prefix)) {
            url = url.replace(prefix, "");
        }
        try {
            return minioClient.getObject(GetObjectArgs.builder()
                    .bucket(bucketName)
                    .object(url)
                    .build());
        } catch (Exception e) {
            log.error("获取文件流信息失败", e);
        }
        return null;
    }
}
